// 获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activity: '',
    sixItemsList: [],
    activity_coves: '', // 活动封面
    popup_switch: false, 
    class_id: '', // 活动ID
    repair_code: "", // 用户填写的邀请码
    contactNumber: '', // 联系获取邀请码
    repair_code_error: false, // 用户填写的邀请码是否错误（true：错了，false：还没填）
    invitation_code: '', // 邀请码
    thisPage: 1, // 当前分页页码
    fontSetSize: 'small', // 用户设置的字体大小（'small'：小，'middle'：中，'large'：大）
  },

  onNavigateTo(e){
    if(parseInt(e.currentTarget.dataset.rest)){
   // 已报名
     if(e.currentTarget.dataset.holiday == 1){    
       wx.navigateTo({
         url:e.currentTarget.dataset.path
       })
     }else{
        this.setData({
         restDay:true
        })
     }   
     
    }else{
     // 没有报名
     wx.navigateTo({
       url:e.currentTarget.dataset.path
     })
    }
  
   } ,

  /**
   * 打开邀请码弹窗
   * @param {*} e 
   */
  handleOpenPopup (e) {
    this.setData({
      popup_switch: true,
      class_id: e.currentTarget.dataset.class_id
    })
  },

  /**
   * 关闭邀请码弹窗
   */
  handleClosePopup () {
    this.setData({
      popup_switch: false,
      repair_code: '',
      repair_code_error: false,
      class_id: ''
    })
  },

  /**
   * 获取填写邀请码
   * @param {*} e 
   */
  getRepairCode (e) {
    this.setData({
      repair_code: e.detail.value
    })
  },

  /**
   * 发送验证邀请码
   * @param {*} e 
   */
  handleSendInvitationCode (e) {
    console.log(this.data.repair_code)
    let token = wx.getStorageSync('token')
    app.http({
      config: {
        url: '/api/User/add_activity_register',
        data: {
          token,
          Invitation_code: this.data.repair_code,
          class_id: this.data.class_id
        },
        method: 'POST'
      },
      isAuth: false
    }).then(res => {
      console.log(res, 'sendCompanyCodeRes')
      this.setData({
        popup_switch: false,
        thisPage: 1,
        sixItemsList: []
      })
      this.getSixItemsActivity()
      wx.showModal({
        title: '提示',
        content: res.data.message,
        showCancel: false,
      })
    }).catch(err => {
      console.log(err, 'sendCompanyCodeErr')
      this.setData({
        repair_code_error: true
      })
    });
  },

  /**
   * 联系获取邀请码
   */
  getContactNumber () {
    let token = wx.getStorageSync('token')
    app.http({
      config: {
        url: '/api/User/phone',
        data: {
          token
        },
        method: 'POST'
      },
      isAuth: false
    }).then(res => {
      console.log(res, 'contactNumberRes');
      let contactNumber = res.data.data
      this.setData({
        contactNumber
      })
    }).catch(err => {
      console.log(err, 'contactNumberErr')
    })
  },

  /**
   * 获取六项精进列表
   */
  getSixItemsActivity () {
    let that = this
    let token = wx.getStorageSync('token')
    // console.log(token)
    app.http({
      config: {
        url: '/api/User/class_list',
        data: {
          token,
          page: that.data.thisPage
        },
        method: 'POST'
      },
      isAuth: true
    }).then(res => {
      console.log(res, 'sixItemsActivityRes')
      let array = res.data.data.data
      array.forEach(item => {
        let created_time = item.start_time.substr(0, 10).replace(/-/g, '.')
        item.created_time = created_time
      })
      let sixItemsList = that.data.sixItemsList == [] ? array : that.data.sixItemsList.concat(array)
      that.setData({
        sixItemsList
      })
      if (that.data.thisPage < res.data.data.last_page) {
        let thisPage = that.data.thisPage + 1
        that.setData({
          thisPage
        })
        that.getSixItemsActivity()
      }
    }).catch(err => {
      console.log(err, 'sixItemsActivityErr')
    });
  },

  /**
   * 获取六项精进列表封面
   */
  getActivityCoves () {
    let token = wx.getStorageSync('token')
    // console.log(token)
    app.http({
      config: {
        url: '/api/User/activity_coves',
        data: {
          token,
          type: 2
        },
        method: 'POST'
      },
      isAuth: true
    }).then(res => {
      console.log(res, 'ActivityCovesRes')
      this.setData({
        activity_coves: res.data.data
      })
    }).catch(err => {
      console.log(err, 'ActivityCovesErr')
    });
  },

  /**
   * 获取屏幕高度 
   */ 
  getSysdata () {
    let that = this
    wx.getSystemInfo({
      success (e) {
        let windowHeight = e.windowHeight
        console.log('windowHeight', windowHeight)
        that.setData({
          windowHeight,
        }, function () {
          // 获取元素宽高
          wx.createSelectorQuery().select('.search').boundingClientRect(function (res) {
            let searchHeight = res.height
            that.setData({
              searchHeight
            })
          }).exec()
        })
      },
      fail (e) {
        console.log(e)
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getContactNumber()
    this.getSysdata()
    app.setTitle('六项精进')
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let fontSetSize = wx.getStorageSync('fontSetSize') // 本地缓存中用户设置的字体大小
    if (fontSetSize) {
      this.setData({
        fontSetSize
      })
    }
    this.setData({
      thisPage: 1,
      sixItemsList: []
    })
    this.getSixItemsActivity()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})